from django import forms
from web import models
from web.forms.widgets import ColorRadioSelect


class BugModelForm(forms.ModelForm):
    """ 创建和编辑BUG的表单 """
    deadline = forms.DateTimeField(
        label='截止日期',
        required=False,
        widget=forms.DateTimeInput(attrs={'type': 'datetime-local'})
    )

    class Meta:
        model = models.Bug
        fields = ['title', 'desc', 'status', 'priority', 'assign_to', 'deadline']
        widgets = {
            'desc': forms.Textarea(attrs={'rows': 4}),
            'status': forms.Select,
            'priority': forms.Select,
            'assign_to': forms.Select,
        }

    def __init__(self, *args, project_id=None, **kwargs):
        super().__init__(*args, **kwargs)
        # 限制状态和优先级只能选择当前项目的选项
        if project_id:
            self.fields['status'].queryset = models.BugStatus.objects.filter(project_id=project_id)
            self.fields['priority'].queryset = models.BugPriority.objects.filter(project_id=project_id)
            # 限制指派给只能选择项目成员
            self.fields['assign_to'].queryset = models.UserInfo.objects.filter(
                projectuser__project_id=project_id
            ).order_by('username')


class BugCommentModelForm(forms.ModelForm):
    """ BUG评论表单 """
    class Meta:
        model = models.BugComment
        fields = ['content']
        widgets = {
            'content': forms.Textarea(attrs={'rows': 3, 'placeholder': '添加评论...'})
        }